package com.i360day.common.security;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * @program: hd_cloud
 * @description: 签名参数
 * @author: liju.z
 * @create: 2020-07-13 16:04
 **/
public class SignUtils {

    public static String simpleSign(Map<String, String> paramsMap){
        List<String> paramNames = new ArrayList<String>(paramsMap.keySet());
        paramNames.remove(SignParamConstant.sign);
        Collections.sort(paramNames);
        StringBuilder paramNameValue = new StringBuilder();
        for (String paramName : paramNames) {
            paramNameValue.append(paramName).append("=").append(paramsMap.get(paramName)).append("&");
        }
        //得到签名字符串
        return Md5Utils.hash(paramNameValue.substring(0, paramNameValue.length() - 1));
    }

    /**
     * <p> @Author liju.z <p>
     *
     * <p> @Description 参数签名 <p>
     *
     * <p> @Date  16:09 <p>
     *
     * <p> @Param [paramsMap, appSecurity] <p>
     *
     * <p> @return [paramsMap, appSecurity] <p>
     *
     **/
    public static String sign(Map<String, String> paramsMap, SecurityModel securityModel){
        List<String> paramNames = new ArrayList<String>(paramsMap.keySet());
        paramNames.remove(SignParamConstant.sign);
        Collections.sort(paramNames);
        StringBuilder paramNameValue = new StringBuilder();
        for (String paramName : paramNames) {
            paramNameValue.append(paramName).append("=").append(paramsMap.get(paramName)).append("&");
        }
        paramNameValue.append(SignParamConstant.appSecurity).append("=").append(securityModel.getAppSecurity());
        //得到签名字符串
        String signStr = CreateSecretKey.sign(paramNameValue.toString(), securityModel.getPrivateKey());
        return Md5Utils.hash(paramNameValue.toString());
    }

    /**
     * <p> @Author liju.z <p>
     *
     * <p> @Description 验证签名 <p>
     *
     * <p> @Date  16:40 <p>
     *
     * <p> @Param [paramsMap, securityModel] <p>
     *
     * <p> @return [paramsMap, securityModel] <p>
     *
     **/
    public static boolean verify(Map<String, String> paramsMap, SecurityModel securityModel){
        String signStr = sign(paramsMap, securityModel);
        String sign = paramsMap.get(SignParamConstant.sign);
        return StrUtil.equals(signStr, sign);
    }

    /**
     * 返回请求参数,如:aa=1&bb=2
     * @param request
     * @return
     */
    public static String getRequestParamStr(HttpServletRequest request) {
        Enumeration<String> params = request.getParameterNames();
        List<String> paramNames = new ArrayList<String>();
        while (params.hasMoreElements()){
            paramNames.add(params.nextElement());
        }
        Collections.sort(paramNames);
        StringBuilder queryString = new StringBuilder();
        for (String key : paramNames) {
            queryString.append(key).append("=").append(request.getParameter(key));
        }
        return queryString.toString();
    }

    /**
     * 返回请求参数,如:aa=1&bb=2
     * @param request
     * @return
     */
    public static Map<String, String> getRequestParamsMap(HttpServletRequest request) {
        Enumeration<String> parameterNames = request.getParameterNames();
        List<String> paramNames = new ArrayList<String>();
        while (parameterNames.hasMoreElements()){
            paramNames.add(parameterNames.nextElement());
        }
        Collections.sort(paramNames);
        Map<String, String> params = new HashMap<String, String>();
        for (String key : paramNames) {
            params.put(key, request.getParameter(key));
        }
        return params;
    }
}
